<?php

namespace app\api\controller;

use think\Controller;
use think\Request;
use \think\Exception;
use app\api\model\Users;
use app\api\model\Orders;

class Order extends Api
{
    public function _initialize()
    {
        $this->model['users']  = new Users;//model('Users');
        $this->model['orders'] = new Orders;//model('Orders');               
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function getIndex()
    {
        $user_id = $this->getTokenUserInfo('id');
        $orders = $this->model['orders']->getOrders(['rent_user_id' => $user_id]);
        $this->setData($orders);
        return $this->send();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function postSave(Request $request)
    {
        $order_info = $request->get();
        try {
            switch ($request->get('order_type')) {
                case 'share':
                    $this->setData($this->model['orders']->createShareOrder($order_info));
                    break;
                case 'wishs':
                    $this->setData($this->model['orders']->createWishOrder($order_info));
                    break;
                case 'rents':
                    $this->setData($this->model['orders']->createRentOrder($order_info));
                    break;
            }
            return $this->send();           
        } catch (\Exception $e) {
            throw new Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function getRead($id)
    {
        try {
            $user_id = $this->getTokenUserInfo('id');
            $this->setData($this->model['orders']->getOrderInfo(['number' => $id, 'rent_user_id' => $user_id]));
            return $this->send();            
        } catch (\Exception $e) {
            throw new Exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function postUpdate(Request $request)
    {
        $number = $request->get('id');
        try {
            $user_id = $this->getTokenUserInfo('id');
            $this->model['orders']->update($request, ['number' => $number, 'rent_user_id' => $user_id], true);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function getDelete($id)
    {
        try {
            $user_id = $this->getTokenUserInfo('id');
            $data = $this->model['orders']->getOrderInfo(['number' => $id, 'rent_user_id' => $user_id]);  
            $data->delete();
            return $this->send();          
        } catch (\Exception $e) {
            throw new Exception($e->getMessage(), $e->getCode());
        }
    }
}
